package com.hxf.service.food.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil; 
import com.hxf.dto.MealMenuDto;
import com.hxf.dto.MealMenuTypeDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.TmpTableDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.impl.BaseServiceImpl;
import com.hxf.service.food.MealMenuService;
import com.hxf.web.vo.MealMenuVo;

@SuppressWarnings("unchecked")
@Repository("mealMenuService")
public class MealMenuServiceImpl extends BaseServiceImpl implements MealMenuService {

	@Override
	public ReturnData<MealMenuDto> findByMealMenu(MealMenuDto mealMenuDto, int targetPage, int pageSize) throws ServiceException {
		try {
			return super.daoFacade.getMealMenuDao().findByMealMenu(mealMenuDto, targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public List<MealMenuDto> findByMealMenu(MealMenuDto mealMenuDto) throws ServiceException {
		try {
			return super.daoFacade.getMealMenuDao().findByMealMenu(mealMenuDto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public List<MealMenuTypeDto> findByMealMenuType(
			MealMenuTypeDto mealMenuTypeDto) throws ServiceException {
		try {
			return super.daoFacade.getMealMenuDao().findByMealMenuType(mealMenuTypeDto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public void updateMealMenu(String ids, boolean isSell) throws ServiceException { 
		try {
			
			if (PublicUtil.isNotNullAndEmpty(ids)) {
				String [] id  = ids.split(",");
				MealMenuDto mmd = null;
				for (String mid : id) {
					mmd = (MealMenuDto)super.daoFacade.getMealMenuDao().get(MealMenuDto.class, Integer.parseInt(mid)); 
					mmd.setIsSell(isSell);
					super.update(mmd);
				}
			}
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}  
	}

	@Override
	public int updateMealMenuStatus(String ids, Integer status) throws ServiceException {
		try { 
			if (PublicUtil.isNotNullAndEmpty(ids)) {
				String [] id  = ids.split(",");
				MealMenuDto rdto = null;
				for (String rid : id) {
					rdto = (MealMenuDto)super.daoFacade.getMealMenuDao().get(MealMenuDto.class, Integer.parseInt(rid)); 
					rdto.setStatus(status);
					super.update(rdto);
				}
			} else {
				return -1;
			}
			return 0;
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e); 
		}  
	}

	@Override
	public ReturnData<MealMenuVo> findMealMenuByRestaurant(RestaurantDto restaurantDto, MealMenuDto mealMenuDto,
			int targetPage, int pageSize) throws ServiceException {
		try {
			return super.daoFacade.getMealMenuDao().findMealMenuByRestaurant(restaurantDto, mealMenuDto, targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public List<MealMenuDto> findByMealMenuByIds(String mealMenuIds)
			throws ServiceException {
		try {
			  /*//使用临时表
			  if (PublicUtil.isNotNullAndEmpty(mealMenuIds)) {
				String [] mealMenuId = mealMenuIds.split(",");
				if (null != mealMenuId && 0 != mealMenuId.length) { 
					List<TmpTableDto> tableDto = new ArrayList<TmpTableDto>();
					TmpTableDto dto = null;
					for (int i = 0;i < mealMenuId.length; i++) { 
						  dto = new TmpTableDto();
						  dto.setValue1(Integer.parseInt(mealMenuId[i]));
						  dto.setValue2("0");
						  tableDto.add(dto);
					}
					super.daoFacade.getBaseDao().deleteTmpTable();
					super.daoFacade.getBaseDao().saveTmpTable(tableDto);
				}
			}*/
			return super.daoFacade.getMealMenuDao().findByMealMenuByIds(mealMenuIds);
		} catch (DaoLevelException e) {
			throw new ServiceException(e);
		}
	} 
}
